//
//  everyDayViewController.m
//  htmlTest
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "everyDayViewController.h"
#import "Request.h"
#import "detaileViewController.h"
#import "diaryViewController.h"
@interface everyDayViewController ()<UITableViewDelegate,UITableViewDataSource,RequestDataDelegate>
@property(nonatomic,retain)NSMutableArray *array;
@property(nonatomic,retain)UITableView *everyDayTV;
//@property(nonatomic,assign)NSInteger day;
//@property(nonatomic,retain)NSDate *babyDate;
@property (nonatomic,retain)NSDate *nowDate;
@property (nonatomic,retain)NSString *nowDateString;
@property (nonatomic,assign)NSInteger dateToBirth;
@end

@implementation everyDayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //属性赋值
    //self.babyDate = _setDay;
    //[self dayDifferOfSetAndBrith];
    
    self.navigationItem.title = @"工具";
    self.navigationController.navigationBar.tintColor = [UIColor brownColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:255 / 256.0 green:204 / 256.0 blue:204 / 256.0 alpha:1];
    self.view.backgroundColor = [UIColor colorWithRed:255/256.0 green:204/256.0 blue:204/256.0 alpha:0.8];
    //创建一个tableview展示每日知识
    self.everyDayTV = [[UITableView alloc]initWithFrame:CGRectMake(20, 0, 335, 176+88) style:(UITableViewStylePlain)];
   self.everyDayTV.backgroundColor = [UIColor colorWithRed:255/256.0 green:204/256.0 blue:204/256.0 alpha:1];
    //设置成不可滑动
    self.everyDayTV.scrollEnabled = NO;
    //设置代理
    _everyDayTV.delegate = self;
    _everyDayTV.dataSource = self;
    //添加父视图
    [self.view addSubview:_everyDayTV];
    
    //请求数据
    //[self layoutRequestData];
    //布置按钮
    [self layoutNineBotton];
}

-(void)viewWillAppear:(BOOL)animated{
    [self getDateData];
    [self layoutRequestData];
    [self.everyDayTV reloadData];
}

//获取最新数据 刷新视图
-(void)getDateData{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    
    self.nowDate = [NSDate date];
    self.nowDateString = [dateFormatter stringFromDate:_nowDate];
    
    NSDate *dateFromBirth = [[NSUserDefaults standardUserDefaults] valueForKey:@"selectData"];
    if (dateFromBirth == nil) {
        [[NSUserDefaults standardUserDefaults] setObject:_nowDate forKey:@"selectData"];
    }
    
    //计算当前时间和预产期的天数差
    long dd = [dateFromBirth timeIntervalSince1970] - [_nowDate timeIntervalSince1970];
    self.dateToBirth = (dd + 86400)/ 86400 + 1;
    
    
}





//计算当天和设定的预产期之间的天数差
//-(void)dayDifferOfSetAndBrith{
//    long dd = [_babyDate timeIntervalSince1970] - [[NSDate date] timeIntervalSince1970];
//    _day = (dd + 86400)/ 86400 + 1;
//    NSLog(@"%ld",(long)_day);
//}
//数据请求
-(void)layoutRequestData{
    //调用函数进行数据请求
    //创建request类对象
    Request *dRequest = [[Request alloc]init];
    dRequest.delegate = self;
    
    NSString *urlStr = [NSString stringWithFormat:@"http://api.ladybirdedu.com/pregnotice/article/getarticle.php?from=android&day=%ld",280-_dateToBirth];
    
    //调用数据请求函数
    [dRequest requestWithUrl:urlStr];
    
}
//请求数据的协议方法
-(void)requestSucess:(NSData *)data{
   
    //解析出大数组
    self.array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    //拿出数组中对应的字典
    
    
    [self.everyDayTV reloadData];
    
}
-(void)requestError:(NSError *)error{
    
}

//9个button
-(void)layoutNineBotton{
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, 176+88, 100, 80)];
    titleLabel.text = @"掐指一测";
    titleLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:titleLabel];
    NSArray *titleBt = @[@"宝宝身高",@"宝宝血型",@"胎儿体重",@"妈妈体重",@"胎梦解析",@"唐氏筛查",@"胎动记录",@"每日一记"];
    for (int i = 0; i<2; i++) {
        for (int j = 0; j<4; j++) {
            UIButton *bt = [UIButton buttonWithType:(UIButtonTypeSystem)];
            bt.frame = CGRectMake(35+j*(60+20), (176+88+80)+i*(50+80), 50, 50);
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(35+j*(60+20), (178+88+80+50)+i*(20+80+30), 80, 20)];
            [self.view addSubview:lable];
            lable.tag = 1100+4*i+j;
            lable.font = [UIFont systemFontOfSize:14];
            bt.tag = 1000+4*i+j;
            if (bt.tag == 1007) {
                [bt addTarget:self action:@selector(modelEveryDayMake:) forControlEvents:UIControlEventTouchUpInside];
                [bt setBackgroundImage:[UIImage imageNamed:@"diary.png"] forState:UIControlStateNormal];
            }
            else if (bt.tag == 1006){
            [bt addTarget:self action:@selector(toDetailePage:) forControlEvents:UIControlEventTouchUpInside];
                [bt setBackgroundImage:[UIImage imageNamed:@"move.png"] forState:UIControlStateNormal];
            }
            
            else if (bt.tag == 1005){
                [bt addTarget:self action:@selector(toDetailePage:) forControlEvents:UIControlEventTouchUpInside];
                [bt setBackgroundImage:[UIImage imageNamed:@"iconfont-nannv.png"] forState:UIControlStateNormal];
            }
            else if (bt.tag == 1004){
                [bt addTarget:self action:@selector(toDetailePage:) forControlEvents:UIControlEventTouchUpInside];
                [bt setBackgroundImage:[UIImage imageNamed:@"dream.png"] forState:UIControlStateNormal];
            }
            else if (bt.tag == 1003){
                [bt addTarget:self action:@selector(toDetailePage:) forControlEvents:UIControlEventTouchUpInside];
                [bt setBackgroundImage:[UIImage imageNamed:@"weightMom.png"] forState:UIControlStateNormal];
            }
            else if (bt.tag == 1002){
                [bt addTarget:self action:@selector(toDetailePage:) forControlEvents:UIControlEventTouchUpInside];
                [bt setBackgroundImage:[UIImage imageNamed:@"weightBaby.png"] forState:UIControlStateNormal];
            }
            else if (bt.tag == 1001){
                [bt addTarget:self action:@selector(toDetailePage:) forControlEvents:UIControlEventTouchUpInside];
                [bt setBackgroundImage:[UIImage imageNamed:@"blood.png"] forState:UIControlStateNormal];
            }
            else if(bt.tag == 1000){
                [bt addTarget:self action:@selector(toDetailePage:) forControlEvents:UIControlEventTouchUpInside];
                [bt setBackgroundImage:[UIImage imageNamed:@"height.png"] forState:UIControlStateNormal];
            }
            //[bt setTitle:titleBt[4*i+j] forState:UIControlStateNormal];
            lable.text = titleBt[4*i+j];
            
            [self.view addSubview:bt];
        }
    }
    
}
-(void)toDetailePage:(UIButton *)bt{
    detaileViewController *detaileV = [[detaileViewController alloc]init];
   
    NSArray *urlArray =@[@"height_test/height",@"blood/blood",@"baby_weight/baby_cal",@"mum_weight/mum_calc",@"taimeng/index",@"tangshai/gender.html",@"baby_movement/movement",@"yundong"];
    NSInteger tagBt = bt.tag - 1000;
    //拼接网址
    
    NSString *url = [NSString stringWithFormat:@"http://tools.seeyouyima.com/front/%@.php?mode=1&info=66&platform=ios&v=1.7.2&device_id=8A494966-7E98-4A74-A0DE-46AE925BA4A9&lang=zh&themeid=0&client=1",urlArray[tagBt]];
    
    detaileV.htmlUrl = url;
    [self.navigationController pushViewController:detaileV animated:YES];
    
    
}

//模态弹出日记界面
-(void)modelEveryDayMake:(UIButton *)bt{
    diaryViewController *diaryVC = [[diaryViewController alloc]init];
    UINavigationController *diaryNVC = [[UINavigationController alloc]initWithRootViewController:diaryVC];
    diaryNVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:diaryNVC animated:YES completion:nil];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor colorWithRed:255 / 256.0 green:204 / 256.0 blue:204 / 256.0 alpha:0.8];
    label.text = @"每日知识";
    label.font = [UIFont systemFontOfSize:20];
    return label;
}
//设置每个分区的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
//设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //重用
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    //取出解析出来的数组中的字典
    NSDictionary *dic = self.array[indexPath.row];
    //把字典的title展示在cell的label上
    cell.textLabel.text = [dic valueForKey:@"title"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.backgroundColor = [UIColor colorWithRed:255/256.0 green:204/256.0 blue:204/256.0 alpha:0.8];
    return cell;
}

//设置每个分区的title
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"每日知识";
}

//选中某一行触发的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    detaileViewController *detaileVC = [[detaileViewController alloc]init];
    NSDictionary *dic = [self.array objectAtIndex:indexPath.row];
    detaileVC.htmlUrl = [dic valueForKey:@"url"];
    [self.navigationController pushViewController:detaileVC animated:YES];
}















@end
